#include <bits/stdc++.h>
using namespace std;

class Solution {
public:
	vector<vector<int>> generateMatrix(int n) {
		vector<vector<int>> result(n,vector<int>(n,0));
		int startX = 0,startY = 0,offset = 1;
		int loop = n / 2;
		int i,j;
		int count = 1;
		while(loop--){
			for(j = startY;j < n - offset;j++){
				result[startX][j] = count++;
			}
			for(i = startX;i < n - offset;i++){
				result[i][j] = count++;
			}
			for(;j > startX;j--){
				result[i][j] = count++;
			}
			for(;i > startY;i--){
				result[i][j] = count++;
			}
			startX++;
			startY++;
			offset++;
		}
		if(n % 2 == 1){
			result[startX][startY] = count;
		}
		return result;
	}
};

class Test{
public:
	vector<vector<int>> generateMatrix(int n) {
		vector<vector<int>> result(n,vector<int>(n,0));
		int loop = n / 2;
		int startX = 0,startY = 0,offset = 1;
		int cnt = 0;
		int i,j;
		while(loop--){
			for(j = startY;j < n - offset;j++){
				result[startX][j] = cnt++;
			}
			for(i = startX;i < n - offset;i++){
				result[i][j] = cnt++;
			}
			for(;j > startY;j--){
				result[i][j] = cnt++;
			}
			for(;i > startX;i--){
				result[i][j] = cnt++;
			}
			startX++;
			startY++;
			offset++;
		}
		if(n % 2 != 0){
			result[startX][startY] = cnt;
		}
		return result;
	}
};


int main(){
	
	Solution s;
	vector<vector<int>> result = s.generateMatrix(5);
	for(int i = 0;i <result.size();i++){
		for(int j = 0;j <result.size();j++){
			cout << result[i][j] << " ";
		}
		cout << endl;
	}
	
	return 0;
}
